package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 根据用户名，修改用户的密码，昵称，年龄
 * 程序启动后，要求用户顺序输入用户名，密码，昵称，年龄
 * 然后完成需求
 */
public class PWDDemo {
    public static void main(String[] args) {
        System.out.println("修改信息");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入同户名：");
        String username  = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        System.out.println("请输入昵称：");
        String nickname = sc.nextLine();
        System.out.println("请输入年龄：");
        int age = Integer.parseInt(sc.nextLine());

        try (
                Connection connection = DBUtil.getConnection();
                ){
          Statement statement = connection.createStatement();
          String sql ="UPDATE user\n" +
                  "SET password ='"+password+"',nickname = '"+nickname+"',age ="+age+"\n" +
                  "WHERE username  ='"+username+" '";
            int num = statement.executeUpdate(sql);
            System.out.println(num>0?"修改成功":"修改失败");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
